layui.config({
  base: "js/"
}).use(['form', 'layer', 'jquery', 'layedit', 'laydate'], function () {
  var form = layui.form(),
    layer = parent.layer === undefined ? layui.layer : parent.layer,
    laypage = layui.laypage,
    layedit = layui.layedit,
    laydate = layui.laydate,
    $ = layui.jquery;

  var course = JSON.parse(sessionStorage.getItem("course"));
  var teachers = new Array();

  console.log("course", course, typeof course);

  $('#Cos_id').val(course.Cos_id);
  $('#Cos_name').val(course.Cos_name);
  $('#Cos_time').val(course.Cos_time);

  $('#Cos_credit').val(course.Cos_credit);
  // $('#Cos_teacher').val(course.Cos_teacher);
  $('#Cos_college').val(course.Cos_college);

  $('#Cos_site').val(course.Cos_site);
  $('#Cos_stime').val(course.Cos_stime);
  $('#Cos_etime').val(course.Cos_etime);

  $('#Cos_maxNum').val(course.Cos_maxNum);
  $('#Cos_intro').val(course.Cos_intro);

  // 渲染教师列表
  $.ajax({
    url: "/api/teacher/all",
    type: "GET",
    dataType: "json",
    success: function (data) {
      console.log(data);
      teachers = data;
      console.log(teachers);
      for (var i = 0; i < teachers.length; i++) {
        console.log(teachers[i]);
        if (course.Cos_teacher === teachers[i].Tea_id)
          $('#Cos_teacher').append('<option value="' + teachers[i].Tea_id + '" selected>' + teachers[i].Tea_name + '(' + teachers[i].Tea_id + ')' + '</option>');
        else
          $('#Cos_teacher').append('<option value="' + teachers[i].Tea_id + '">' + teachers[i].Tea_name + '(' + teachers[i].Tea_id + ')' + '</option>');
      }
      form.render();
    },
    error: function (data) {
      layer.warn("获取教师列表失败!");
    }
  });

  // $('#Cos_teacher').each(function () {
  //   $(this).val() ===
  // });

  //创建一个编辑器
  var editIndex = layedit.build('Cos_intro');
  form.on("submit(editCourse)", function (data) {
    data.field.Cos_intro = layedit.getContent(editIndex);
    //弹出loading
    var index = layer.msg('数据提交中，请稍候', {icon: 16, time: 3000, shade: 0.8});
    $.ajax({
      url: "/api/course/" + data.field.Cos_id,
      type: "PUT",
      data: data.field,
      dataType: "json",
      success: function (data) {
        console.log(data);
        layer.close(index);
        top.layer.msg("修改成功！");
        layer.closeAll("iframe");
      },
      error: function (data) {
        layer.close(index);
        top.layer.msg("修改失败！");
      }
    });
    return false;
  })

});
